import app from "../../../../app";
import { player, wallet_key } from "../../../../model/fight/body/player";
import { group, EffectType } from "../../../../shared/interface";
import { template } from "../../../../shared/master/MsgAction";

class Roulette {
    games: { player: player[], gold: number, bullet: number, time: number } | undefined = undefined;
    private timer:any = undefined;
    private bullet:number[] = []
    constructor() {
        setInterval(() => {
            this.tick()
        }, 1000)
    }
    create(p: player, gold: number) {
        if (this.games) {
            p.sendMsg(template.default_none, '🔫已经有正在进行中的对局了,请耐心等待');
            return;
        }
        let res = p.check_wallet(gold, 0, [], false, '澳门·轮盘', true);
        if (!res.state) {
            return;
        }
        this.games = {
            player: [p],
            gold: gold,
            bullet: 1,
            time: 30
        }
        this.bullet.push(1);
        p.sendMsg(template.default_none, '🔫对局创建成功,等待其他玩家加入15秒内没有人加入则退回💰金币');
        let list = p.getPosition().getBodys()
        for (let i = 0; i < list.length; i++) {
            const element = list[i];
            if(!element){
                continue;
            }
            if(element instanceof player){
                element.sendMsg(template.default_none,`🔫${p.nickName}创建了${gold}💰的对局！加入发送[加入轮盘]`)
            }
        }
        // 创建对局后 30秒内没有人加入那么退回金币
        this.timer = setTimeout(()=>{
            if(this.games && this.games.player.length == 1){
                p.wallet_change(wallet_key.gold,this.games.gold)
                p.sendMsg(template.default_none,`15秒内没有人加入,💰金币${this.games.gold}已退回`)
                this.games = undefined;
            }
        },15000)
    }
    tick() {
        // 通知位置上的所有人  开枪消息
    }
    addbullet(p: player) {
        if(!this.games){
            p.sendMsg(template.default_none,`🔫当前还没有人创建对局哦`);
            return;
        }
        if(this.games.player.length < 2){
            p.sendMsg(template.default_none,`🔫游戏还没有人加入哦`);
            return;
        }
        if(this.games.player[0].uuid != p.uuid && this.games.player[1].uuid != p.uuid){
            p.sendMsg(template.default_none,`🔫你不能给他人的对局增加子弹`);
            return;
        }
        if(this.games.bullet >= 5){
            p.sendMsg(template.default_none,`🔫子弹最多5发！`);
            return;
        }
        this.games.bullet += 1;
        this.bullet.push(1);
        let list = p.getPosition().getBodys()
        for (let i = 0; i < list.length; i++) {
            const element = list[i];
            if(!element){
                continue;
            }
            if(element instanceof player){
                element.sendMsg(template.default_none,`${p.nickName}给左轮装填了一发子弹!当前子弹数🔥${this.games.bullet}`)
            }
        }
    }
    menu(p: player) {
        p.sendMsg(template.俄罗斯轮盘,{})
    }
    private start(){
        clearInterval(this.timer);
        for (let i = 0; i < 6; i++) {
            const element = this.bullet[i];
            if(!element){
                this.bullet.push(0)
            }
        }
        app.shuffleArray(this.bullet)
        let idx = 0;
        this.timer = setInterval(()=>{
            if(!this.games){
                debugger;
                return;
            }
            let tag = (idx+1)%2;
            let msg = `${this.games.player[tag].nickName}拿起了手枪缓慢的扣动了扳机,`;
            if(this.bullet[idx]){
                msg += `\n☠️${this.games.player[tag].nickName}被枪击了!\n`;
                let win = tag == 1?0:1;
                let gold = Math.ceil(this.games.gold) + 100;
                this.games.player[win].wallet_change(wallet_key.gold,gold)
                msg += `🎉${this.games.player[win].nickName}是最后的赢家🎉\n💰+${gold}`
                clearInterval(this.timer);
                this.timer = undefined;

                this.games.player[tag].add_hp({
                    val: -999999,
                    use_uuid: "1",
                    use_name: '轮盘见证人',
                    sk_name: '魔焰',
                    group: group.主场,
                    type: EffectType.none
                },true)
            }else{
                msg += `🙏这次没有子弹!`;
            }

            let list = this.games.player[0].getPosition().getBodys()
            for (let i = 0; i < list.length; i++) {
                const element = list[i];
                if(!element){
                    continue;
                }
                if(element instanceof player){
                    element.sendMsg(template.default_none,msg)
                }
            }
            if(this.bullet[idx]){
                this.games = undefined;
            }
            idx += 1;

        },3000)
    }
    join(p: player) {
        if(!this.games){
            p.sendMsg(template.default_none,`🔫当前还没有人创建对局哦`);
            return;
        }
        if(this.games.player.length >= 2){
            p.sendMsg(template.default_none,`🔫对局已开始,请耐心等待结束`);
            return
        }
        if(this.games.player[0].uuid == p.uuid){
            p.sendMsg(template.default_none,`🔫您已创建了对局,不能再加入对局哦`);
            return;
        }
        let res = p.check_wallet(this.games.gold, 0, [], false, '澳门·轮盘', true);
        if (!res.state) {
            return;
        }
        this.games.time = 10;
        this.games.gold += this.games.gold;
        this.games.player.push(p);
        let list = p.getPosition().getBodys()
        for (let i = 0; i < list.length; i++) {
            const element = list[i];
            if(!element){
                continue;
            }
            if(element instanceof player){
                element.sendMsg(template.default_none,`🔫游戏即将开始！\n双方10秒内可以选择继续装填子弹\n[${p.nickName}]VS[${this.games.player[0].nickName}]\n奖金💰${this.games.gold}!`)
            }
        }
        this.timer = setInterval(()=>{
            if(!this.games){
                debugger;
                return;
            }
            let list = p.getPosition().getBodys()
            this.games.time -= 1;
            if(this.games.time <= 0){
                for (let i = 0; i < list.length; i++) {
                    const element = list[i];
                    if(!element){
                        continue;
                    }
                    if(element instanceof player){
                        element.sendMsg(template.default_none,`🔫游戏开始！\n[${p.nickName}]vs[${this.games.player[0].nickName}]`)
                    }
                }
                this.start()
                return
            }
            for (let i = 0; i < list.length; i++) {
                const element = list[i];
                if(!element){
                    continue;
                }
                if(element instanceof player){
                    element.sendMsg(template.default_none,`🔫等待双方装填子弹中...倒计时${this.games.time}`)
                }
            }
        },1500)
    }
}
export default new Roulette();